using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using Voodoo.Pattern;

public class CityBuildingManager : MonoBehaviour
{
	public Action<CityBuildingElementHandler> OnElementSelected;

	public Action<bool> OnRVX2;

	public Action OnCollectGems;

	public List<GameObject> Sounds;

	public Sprite GemSprite;

	public Sprite CashSprite;

	public LayerMask LayerMask;

	public UIAudio UIAudio;

	public GameplayAudio GameplayAudio;

	private static CityBuildingManager _instance;

	public const float MULTIPLY_DURATION_MINUTES = 5f;

	public const int MULTIPLY_VALUE = 3;

	public const string X2_RVTAKENDATE = "VoodooCBX2RV";

	private bool _rvX2Enable;

	private CityBuildingElementHandler _currentClickedHandler;

	private Vector2 _screenStart;

	public bool SoundEnable => NintendoPlayerPrefs.GetInt("Sound", 1) == 1;

	public int Money
	{
		get
		{
			return (int)NintendoPlayerPrefs.GetFloat("Money", 0f);
		}
		set
		{
			NintendoPlayerPrefs.SetFloat("Money", value);
		}
	}

	public int Gems
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("Gems", 0);
		}
		set
		{
			NintendoPlayerPrefs.SetInt("Gems", value);
			OnCollectGems?.Invoke();
		}
	}

	public int CurrentLevel
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("Level", 0);
		}
		set
		{
			NintendoPlayerPrefs.SetInt("Level", value);
		}
	}

	public static CityBuildingManager Instance
	{
		get
		{
			if (!_instance)
			{
				_instance = UnityEngine.Object.FindObjectOfType<CityBuildingManager>();
			}
			return _instance;
		}
	}

	public DateTime LastX2CollectPref
	{
		get
		{
			string @string = NintendoPlayerPrefs.GetString("VoodooCBX2RV", "");
			if (string.IsNullOrEmpty(@string))
			{
				LastX2CollectPref = DateTime.Now.AddDays(-10.0);
				return LastX2CollectPref;
			}
			return DateTime.FromOADate(double.Parse(@string));
		}
		set
		{
			NintendoPlayerPrefs.SetString("VoodooCBX2RV", value.ToOADate().ToString());
		}
	}

	private void Awake()
	{
		_instance = this;
		_rvX2Enable = IsX2Enable();
	}

	private void Start()
	{
		if (_rvX2Enable)
		{
			OnRVX2?.Invoke(obj: true);
		}
		for (int i = 0; i < Sounds.Count; i++)
		{
			if (SoundEnable)
			{
				Sounds[i].GetComponent<ISwichSound>().SoundOn();
			}
			else
			{
				Sounds[i].GetComponent<ISwichSound>().SoundOff();
			}
		}
	}

	public void DoHaptic()
	{
		if ((bool)SingletonMB<GameManager>.Instance)
		{
			SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Big);
		}
	}

	public void DoLightHaptic()
	{
		if ((bool)SingletonMB<GameManager>.Instance)
		{
			SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Small);
		}
	}

	[ExposeMethodInEditor(false, false)]
	private void GiveMoney()
	{
		Money += 100;
		Refresh();
	}

	[ExposeMethodInEditor(false, false)]
	private void GiveGems()
	{
		Gems += 10;
	}

	[ExposeMethodInEditor(false, false)]
	private void AddLevel()
	{
		CurrentLevel++;
		Refresh();
	}

	[ExposeMethodInEditor(false, false)]
	private void DoReset()
	{
		CurrentLevel = 0;
		Money = 0;
		Gems = 0;
		Refresh();
	}

	[ExposeMethodInEditor(false, false)]
	private void Refresh()
	{
		CityBuildingElementHandler[] array = UnityEngine.Object.FindObjectsOfType<CityBuildingElementHandler>();
		for (int i = 0; i < array.Length; i++)
		{
			array[i].Refresh();
		}
	}

	public void Select(CityBuildingElementHandler element)
	{
		OnElementSelected?.Invoke(element);
	}

	public void OnX2GenerationClicked()
	{
		LastX2CollectPref = DateTime.Now;
		_rvX2Enable = true;
		OnRVX2?.Invoke(obj: true);
	}

	public bool IsX2Enable()
	{
		return (DateTime.Now - LastX2CollectPref).TotalMinutes <= 5.0;
	}

	private void Update()
	{
		if (Input.GetMouseButtonDown(0))
		{
			if (!IsOverUI())
			{
				_screenStart = Input.mousePosition;
				Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
				if (Physics.Raycast(ray.origin, ray.direction, out var hitInfo, 10000f, LayerMask))
				{
					_currentClickedHandler = hitInfo.collider.GetComponent<CityBuildingElementHandler>();
				}
			}
		}
		else if (Input.GetMouseButtonUp(0) && !IsOverUI() && Vector2.Distance(Input.mousePosition, _screenStart) <= (float)(Screen.width / 8))
		{
			Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
			if (Physics.Raycast(ray2.origin, ray2.direction, out var hitInfo2, 10000f, LayerMask))
			{
				CityBuildingElementHandler component = hitInfo2.collider.GetComponent<CityBuildingElementHandler>();
				if (component != null && _currentClickedHandler == component)
				{
					component.Select();
				}
			}
		}
		if (_rvX2Enable && !IsX2Enable())
		{
			_rvX2Enable = false;
			OnRVX2?.Invoke(obj: false);
		}
	}

	public bool IsOverUI()
	{
		if ((bool)EventSystem.current)
		{
			return EventSystem.current.IsPointerOverGameObject(0);
		}
		return true;
	}
}
